package mongodb;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.eeuse.EeUseApplication;
import com.example.eeuse.mapper.VedaSnapshotMapper;
import com.example.eeuse.model.EsVedaSnapshot;
import com.example.eeuse.mongodb.dao.VedaSnapshotRepository;
import com.example.eeuse.mongodb.po.MongodbVedaSnapshot;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@SpringBootTest(classes = EeUseApplication.class)
public class CreateData {

    @Resource
    private VedaSnapshotRepository vedaSnapshotRepository;


    @Autowired
    private VedaSnapshotMapper vedaSnapshotMapper;
    @Test
    public void testInsert()
    {
        String val=FileUtil.readString("D:\\test\\SAMPLE\\SAMPLE_RESPONSE\\CAN_HGH_20231213_2023-11-20T09M59M53.391_7c8c7b8fac254f2a9a853d0678354c7b.txt","utf-8");
        long start=System.currentTimeMillis();
        for(int i=0;i<10000;i++) {
            MongodbVedaSnapshot mongodbVedaSnapshot = new MongodbVedaSnapshot(i+"", val);
            vedaSnapshotRepository.insert(mongodbVedaSnapshot);
            if(i%500==0)
            log.info("i={},fee={}",i,System.currentTimeMillis()-start);
        }
        log.info(System.currentTimeMillis()-start+"");
    }

    @Test
    public void testFind()
    {
        long start=System.currentTimeMillis();
        for(int i=0;i<1000;i++) {
            int id=RandomUtil.getRandom().nextInt(0,999);
            EsVedaSnapshot esVedaSnapshot = vedaSnapshotMapper.selectById(id + "");
            if(esVedaSnapshot==null)
            {
                log.info("i={} is null",i);
            }
        }
        log.info(System.currentTimeMillis()-start+"");
    }

    @Test
    public void testMongodbFind()
    {
        long start=System.currentTimeMillis();
        for(int i=0;i<2000;i++) {
            int id=RandomUtil.getRandom().nextInt(0,9999);
            Optional optional = vedaSnapshotRepository.findById(id + "");
            if(!optional.isPresent())
            {
                log.info("i={} is null",i);
            }

        }
        log.info(System.currentTimeMillis()-start+"");
    }
    @Test
    public void findAndSaveMongodb()
    {
        String val=FileUtil.readString("D:\\test\\SAMPLE\\SAMPLE_RESPONSE\\CAN_HGH_20231213_2023-11-20T09M59M53.391_7c8c7b8fac254f2a9a853d0678354c7b.txt","utf-8");
        long start=System.currentTimeMillis();
        for(int i=0;i<1000;i++) {
           Optional<MongodbVedaSnapshot> optional= vedaSnapshotRepository.findById(i + "");
           optional.ifPresent(v->{
               MongodbVedaSnapshot mongodbVedaSnapshot = optional.get();
               if("".equals(mongodbVedaSnapshot.getVal()))
               {
                   log.info("has={}", mongodbVedaSnapshot.getId());
               }
           });
           if(!optional.isPresent())
           {
               MongodbVedaSnapshot mongodbVedaSnapshot = new MongodbVedaSnapshot(i+"", val);
               vedaSnapshotRepository.insert(mongodbVedaSnapshot);
               log.info("not exist={}",i);
           }
        }
        log.info(System.currentTimeMillis()-start+"");

    }


    @Test
    public void testInsertEs()
    {
        String val=FileUtil.readString("D:\\test\\CAN_HGH_20231126_2023-10-30T05M50M36.792_5e5fbea1f8d94b78a3e321bc0e902107.txt","utf-8");
        long start=System.currentTimeMillis();
        for(int i=1000;i<10000;i++) {
           insert(i+"",val);
        }
        log.info(System.currentTimeMillis()-start+"");
    }

    public void insert(String i,String val)
    {
        log.info("insert={}",i);
        EsVedaSnapshot vedaSnapshot = new EsVedaSnapshot(i, val);
        try {
            vedaSnapshotMapper.insert(vedaSnapshot,"snapshot-veda");
        }
        catch (Exception e)
        {

        }

    }
    @Test
    public void testFineAndSaveEs()
    {
        List<String> list=new ArrayList<>();
        String val=FileUtil.readString("D:\\test\\CAN_HGH_20231126_2023-10-30T05M50M36.792_5e5fbea1f8d94b78a3e321bc0e902107.txt","utf-8");
        for(int i=7828;i<10000;i++) {
           EsVedaSnapshot vedaSnapshot = vedaSnapshotMapper.selectById(i+"");
           if(vedaSnapshot ==null)
           {
               log.info("no exist,save={}",i);
               insert(i+"",val);
           }
        }
    }
}
